package phase03.module01.code;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @author 张富强
 * 将mapper传过来的已经拍好序的分区数据合并到一起，因为只有一个reducer，使用类成员变量count记录排名，而key直接作为value输出
 */
public class SortNumbersReducer extends Reducer<IntWritable, NullWritable, IntWritable, IntWritable> {
    private int count = 1;

    @Override
    protected void reduce(IntWritable key, Iterable<NullWritable> values, Reducer<IntWritable, NullWritable, IntWritable, IntWritable>.Context context) throws IOException, InterruptedException {
        for (NullWritable val : values) {
            context.write(new IntWritable(count), key);
            count += 1;
        }
    }
}
